import React, { Component } from 'react'
import PubSub from "pubsub-js"
import axios from "axios"

export default class Search extends Component {
    search = () => {

        const { keyWordElement: { value: keyWord } } = this
        // console.log(keyWord)
        this.props.updateAppState({
            isFirst: false,
            isLoading: true
        })
        PubSub.publish("atguigu",{
            isFirst:false,
            isLoading:true
        })
        axios.get(`/api1/search/users2?q=${keyWord}`).then(res => {
            // this.props.updateAppState({
            //     isLoading: false,
            //     userList: res.data.items
            // })
            PubSub.publish("atguigu", {
                isLoading: false,
                userList: res.data.items
            })
        }).catch(err =>
            PubSub.publish("atguigu", {
                isLoading: false, err: err.message
            }
            ))
    }
    render() {
        return (
            <section className="jumbotron">
                <h3 className="jumbotron-heading">搜索 Github 用户</h3>
                <div>
                    <input ref={c => this.keyWordElement = c} type="text" placeholder="输入关键词" />&nbsp;<button onClick={this.search}>搜索</button>
                </div>
            </section>

        )
    }
}
